package com.ruoyi.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.pojo.Coupon;
import com.ruoyi.pojo.R;
import com.ruoyi.service.CouponService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author: liufei
 * @Desc:
 * @create: 2024-11-04 09:04
 **/
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/couponList")
    public R list(@RequestHeader String token){

        String couponList1 = stringRedisTemplate.opsForValue().get("couponList");

        HashMap<String, Object> hashMap = new HashMap<>();

        Claims claims = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();

        String role = claims.get("role", String.class);

        if (StringUtils.isEmpty(couponList1)){

            List<Coupon> couponList = couponService.list();

            stringRedisTemplate.opsForValue().set("couponList", JSONArray.toJSONString(couponList));

            hashMap.put("couponList", couponList);
            hashMap.put("role", role);
            System.out.println("第一次查询存入redis");

        }else{
            System.out.println("从redis中查询数据");
            JSONArray list = JSONArray.parseArray(couponList1);
            hashMap.put("couponList", list);
            hashMap.put("role", role);

        }

        return R.ok(hashMap);
    }

    @PostMapping("/AddCoupon")
    public R addCoupon(@RequestBody Coupon coupon,@RequestHeader String token){

        Claims claims = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();

        Integer userId = claims.get("userId", Integer.class);

        String user = stringRedisTemplate.opsForValue().get("coupon:" + userId);

        if(StringUtils.isEmpty(user)){
            stringRedisTemplate.opsForValue().set("coupon:" + userId, JSON.toJSONString(coupon));

            LambdaUpdateWrapper<Coupon> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Coupon::getId, coupon.getId()).setSql("nums = nums - 1");
            couponService.update(updateWrapper);

            List<Coupon> couponList = couponService.list();
            stringRedisTemplate.opsForValue().set("couponList", JSON.toJSONString(couponList));

            return R.ok();
        }else{
            return R.error("该用户已领取优惠券");
        }
    }

    /**
     * 每天 0点执行
     */
//    @Scheduled(cron = "0 0 0 * * ?")
    @Scheduled(cron = "0 0/1 * * * ?")
    public void updateSurplusNumber(){

        List<Coupon> couponList = couponService.list();

        stringRedisTemplate.opsForValue().set("couponList", JSON.toJSONString(couponList));

        System.out.println("每天0点定时更新");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");
        System.out.println("测试测试测试");


    }



}
